package com.work.csms.mapper;
import com.work.csms.dao.Fix;
import com.work.csms.dao.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FixMapper {

    @Select({"<script>" +
            "SELECT f.*,u.name FROM fix f left join user u on f.userid = u.userid " +
            " where 1 = 1" +
            "<if test=\"des != null and des != '' \"> " +
            " AND f.des like concat('%',#{des},'%')" +
            "</if>" +
            "<if test=\"name != null and name != '' \"> " +
            " AND u.name like concat('%',#{name},'%')" +
            "</if>" +
            " limit #{offset} , #{size}" +
            "</script>"})
    List<Fix> page(String des, String name, Integer offset, Integer size);

    @Select({"<script>" +
            "SELECT f.*,s.name FROM `fix` f left join station s on f.stationid = s.id  " +
            "where 1 = 1" +
            "<if test=\"userid != null\"> " +
            " AND f.userid = #{userid}" +
            "</if>" +
            "<if test=\"statu != null and statu != '' \"> " +
            " AND f.statu = #{statu}" +
            "</if>" +
            " order by start desc " +
            "</script>"})
    List<Fix> list(Fix fix);

    @Select({"<script>" +
            "SELECT count(0) FROM fix " +
            " where start  &gt;= #{start} and start &lt;= #{end}" +
            "</script>"})
    Integer getFixCountByDate(Fix fix);


    @Select({"<script>" +
            "SELECT count(0) FROM fix " +
            "where 1 = 1" +
            "<if test=\"id != null and id != '' \"> " +
            " AND f.des like concat('%',#{des},'%')" +
            "</if>" +
            "<if test=\"name != null and name != '' \"> " +
            " AND u.name like concat('%',#{name},'%')" +
            "</if>" +
            "</script>"})
    Integer count(Integer id);

    @Select("SELECT * FROM fix WHERE id=#{id}")
    Fix getFixById(Integer id);


    @Update({"<script>" +
            "update fix set " +
            "<if test=\"userid != null \"> " +
            " userid = #{userid}," +
            "</if>" +
            "<if test=\"stationid != null \"> " +
            " stationid = #{stationid}," +
            "</if>" +
            "<if test=\"des != null and des != '' \"> " +
            " des = #{des}," +
            "</if>" +
            "<if test=\"statu != null and statu != '' \"> " +
            " statu = #{statu}," +
            "</if>" +
            "<if test=\"start != null \"> " +
            " start = #{start}," +
            "</if>" +
            "<if test=\"end != null \"> " +
            " end = #{end}," +
            "</if>" +
            " id = #{id}" +
            " where id = #{id}" +
            "</script>"})
    void update(Fix fix);

    @Insert("INSERT INTO fix(userid, stationid, des, statu, start, end) VALUES " +
            "(#{userid}, #{stationid}, #{des}, #{statu}, #{start}, #{end})")
    void save(Fix fix);

    @Delete("delete from fix where id = #{id} ")
    void deleted(Integer id);
}
